package com.example.wxjava.util;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 事件路由配置
 */
@Configuration
public class WxJavaRooterConfig {

    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private SubscribeHandler subscribeHandler;

    @Autowired
    private TextHandler textHandler;

    @Bean
    public WxMpMessageRouter messageRouter(){
        //创建消息路由
        final WxMpMessageRouter router = new WxMpMessageRouter(wxMpService);

        //添加相应关注 文字路由
        router.rule().async(false).msgType(WxConsts.XmlMsgType.TEXT).handler(textHandler).end();

        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE).handler(subscribeHandler).end();

        return router;
    }
}
